package cn.aifeix.order.service.impl;

import cn.aifeix.basic.config.BussinessException;
import cn.aifeix.order.domain.OrderCancelLog;
import cn.aifeix.order.domain.OrderPetAcquisition;
import cn.aifeix.order.dto.AcquisitionOrderFormDto;
import cn.aifeix.order.mapper.OrderPetAcquisitionMapper;
import cn.aifeix.order.service.IOrderCancelLogService;
import cn.aifeix.order.service.IOrderPetAcquisitionService;
import cn.aifeix.basic.service.impl.BaseServiceImpl;
import cn.aifeix.org.domain.Shop;
import cn.aifeix.pet.domain.Pet;
import cn.aifeix.pet.domain.PetDetail;
import cn.aifeix.pet.domain.PetType;
import cn.aifeix.pet.domain.SearchMasterMsg;
import cn.aifeix.pet.service.IPetService;
import cn.aifeix.pet.service.ISearchMasterMsgService;
import cn.aifeix.pet.service.impl.SearchMasterMsgServiceImpl;
import cn.aifeix.user.domain.User;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aifeix
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IPetService petService;

    @Autowired
    private IOrderCancelLogService orderCancelLogService;
    /**
     * 确认提交订单
     * @param dto
     */
    @Override
    @Transactional
    public void confirm(AcquisitionOrderFormDto dto) {
        // 根据id查询订单信息
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(dto.getId());
        if (null == orderPetAcquisition){
            throw new BussinessException("订单不存在");
        }
        // 查询寻主消息 修改状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(3);
        searchMasterMsgService.update(searchMasterMsg);
        // 新增宠物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(orderPetAcquisition.getPrice().toString());
        pet.setTypeId(1L);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petService.add(pet);
        // 修改订单状态为完成 设置金额
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(dto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }

    /**
     * 取消订单
     * @param orderId
     */
    @Override
    @Transactional
    public void cancel(Long orderId) {
        // 根据id查询订单信息
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderId);
        if (null == orderPetAcquisition){
            throw new BussinessException("订单不存在");
        }
        // 修改订单状态为取消-1
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 查询寻主消息，修改寻主消息状态为1，清空shopId
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgService.update(searchMasterMsg);
        // 保存取消订单日志
        OrderCancelLog orderCancelLog = OrderCancelLog.builder()
                .empId(orderPetAcquisition.getEmpId())
                .msgId(orderPetAcquisition.getSearchMasterMsgId())
                .orderSn(orderPetAcquisition.getOrderSn())
                .cancelTime(new Date())
                .shopId(orderPetAcquisition.getShopId())
                .build();
        orderCancelLogService.add(orderCancelLog);
    }
}
